
//
//  PMineCreditEvaluationViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/23.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineCreditEvaluationViewController.h"
#import "LYAvatarCertificationController.h"
#import "RealNameAuthenticationViewController.h"

#import "LYAcademicCertificateListViewController.h"

#import "LYCreditEvaluationView.h"


@interface PMineCreditEvaluationViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *beatLabel;

//实名
@property (nonatomic, strong) UIImageView *verifiedImage;
@property (nonatomic, strong) UIButton *verifiedBtn;
//头像
@property (nonatomic, strong) UIImageView *avatarcerImage;
@property (nonatomic, strong) UIButton *avatarcerBtn;

//学历
@property (nonatomic, strong) UIButton *educationBtn;
//工作
@property (nonatomic, strong) UIButton *jobBtn;

@property (nonatomic, strong) NSDictionary *dataDic;

@property (nonatomic, strong) LYCreditEvaluationView *circleProgessView2;

@property (nonatomic, strong) UILabel *evaluatCountlabel;
@property (nonatomic, strong) UILabel *evaluationlabel;
@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, assign) NSInteger evaluationNumber;

@end

@implementation PMineCreditEvaluationViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self requestData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"职场信用评估";
    [self initUI];
}

- (void)initUI {
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.hidden = YES;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = RGB(247, 247, 247);
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableview];
    
    UIView *headerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 680)];
    headerview.backgroundColor = [UIColor whiteColor];
    self.tableview.tableHeaderView = headerview;
    //信用
    UIImageView *evaluatBackView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    evaluatBackView.image = [UIImage imageNamed:@"xinyong-new-bg"];
    [headerview addSubview:evaluatBackView];
    
    LYCreditEvaluationView *circleProgessView2 = [[LYCreditEvaluationView alloc] initWithFrame:CGRectMake((WIDTH-180)/2, 40, 180, 180)];
    circleProgessView2.backgroundColor = [UIColor clearColor];
    circleProgessView2.fillLineColor = [UIColor whiteColor];
    circleProgessView2.lineWidth = 10;
    circleProgessView2.lineColor = RGB(206, 116, 35);
    self.circleProgessView2 = circleProgessView2;
    __weak typeof(self) weakSelf = self;
    circleProgessView2.block = ^(NSString *text){
        weakSelf.evaluatCountlabel.text = text;
    };
    [headerview addSubview:circleProgessView2];
    
    self.evaluatCountlabel = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 100)/2, circleProgessView2.bottom - 75, 100, 30)];
    self.evaluatCountlabel.textColor = [UIColor whiteColor];
    self.evaluatCountlabel.font = [UIFont boldSystemFontOfSize:30];
    self.evaluatCountlabel.textAlignment = NSTextAlignmentCenter;
    [headerview addSubview:self.evaluatCountlabel];
    
    self.evaluationlabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.evaluatCountlabel.bottom + 10, WIDTH - 30, 30)];
    self.evaluationlabel.textColor = [UIColor whiteColor];
    self.evaluationlabel.font = [UIFont boldSystemFontOfSize:18];
    self.evaluationlabel.textAlignment = NSTextAlignmentCenter;
    [headerview addSubview:self.evaluationlabel];

    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, self.evaluationlabel.bottom + 10, WIDTH - 30, 20)];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    [headerview addSubview:self.timeLabel];
    
    //头像背景
    UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(32, evaluatBackView.bottom-48 , WIDTH-64, 84)];
    shadowView.layer.cornerRadius = shadowView.height/2;
    shadowView.clipsToBounds = YES;
    [shadowView colorgradient:CGPointMake(0, 1) startColor:RGB(244,244,244) endPoint:CGPointMake(1, 1) endColor:RGB(246,246,246)];
    [headerview addSubview:shadowView];
    
    UIView *orangeBackView = [[UIView alloc] initWithFrame:CGRectMake(35, evaluatBackView.bottom-45, WIDTH-70, 78)];
    orangeBackView.backgroundColor = [UIColor whiteColor];
    orangeBackView.layer.cornerRadius = orangeBackView.height/2;
    orangeBackView.clipsToBounds = YES;
    [orangeBackView addShadowColor:[UIColor blackColor] offset:CGSizeMake(2, 5)];
    [headerview addSubview:orangeBackView];
    
    self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 9, 60, 60)];
    self.iconView.layer.cornerRadius = self.iconView.height/2;
    self.iconView.clipsToBounds = YES;
    [orangeBackView addSubview:self.iconView];
    
    self.beatLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.iconView.right + 5, 14, orangeBackView.width - self.iconView.right -10, 25)];
    self.beatLabel.font = Font(Font_13);
    self.beatLabel.textColor = THEMECOLOR_OF_TEXT_333333;
    self.beatLabel.adjustsFontSizeToFitWidth = YES;
    [orangeBackView addSubview:self.beatLabel];
    
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.beatLabel.left, self.beatLabel.bottom , self.beatLabel.width, 30)];
    messageLabel.font = Font(Font_14);
    messageLabel.textColor = RGB(34, 34, 34);
    messageLabel.text = @"提升信用分，让更多的HR看到您";
    messageLabel.adjustsFontSizeToFitWidth = YES;
    [orangeBackView addSubview:messageLabel];
    
    //信用特权
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(orangeBackView.left, orangeBackView.bottom + 40, 2, 10)];
    lineView1.backgroundColor = [UIColor blackColor];
    [headerview addSubview:lineView1];
    
    UILabel *privilegeLabel = [[UILabel alloc] initWithFrame:CGRectMake(lineView1.right+5, lineView1.top - 5, WIDTH - lineView1.right -20, 20)];
    privilegeLabel.font = [UIFont boldSystemFontOfSize:14];
    privilegeLabel.textColor = RGB(34, 34, 34);
    privilegeLabel.text = @"信用特权";
    [headerview addSubview:privilegeLabel];
    
    
    CGFloat width = 60;
    CGFloat height = 110;
    CGFloat margin = (WIDTH - 120)/3;
    CGFloat dy = privilegeLabel.bottom + 15;
    
    NSArray *iconArray = @[@"xinyong-shipin", @"xinyong-v", @"xinyong-youxian", @"xinyong-zhuanqian"];
    NSArray *titleArray = @[@"视频面试特权", @"信用认证标示", @"简历优先展示", @"简历可以赚钱"];
    for (NSInteger i = 0; i<iconArray.count; i ++) {
        NSInteger row = i%2;
        NSInteger column = (row==1)? i/2 : (i+1)/2;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((row==0)?margin:margin*2+width,dy+column*height, width, width+8)];
        imageView.image = [UIImage imageNamed:iconArray[i]];
        [headerview addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(imageView.left-20, imageView.bottom +5, width+40, 20)];
        label.font = [UIFont systemFontOfSize:14];
        label.adjustsFontSizeToFitWidth = YES;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = RGB(34, 34, 34);
        label.text = titleArray[i];
        [headerview addSubview:label];
    }
    
    UIView *footerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 380)];
    footerview.backgroundColor = [UIColor whiteColor];
    self.tableview.tableFooterView = footerview;
    
    //信用认证
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(25, 10, 2, 10)];
    lineView2.backgroundColor = [UIColor blackColor];
    [footerview addSubview:lineView2];
    
    UILabel *certificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(lineView2.right+5, lineView2.top - 5, WIDTH - lineView1.right -20, 20)];
    certificationLabel.font = [UIFont boldSystemFontOfSize:14];
    certificationLabel.textColor = RGB(34, 34, 34);
    certificationLabel.text = @"信用认证";
    [footerview addSubview:certificationLabel];
    
    self.verifiedImage = [[UIImageView alloc] initWithFrame:CGRectMake(margin, certificationLabel.bottom + 15, width, width)];
    [footerview addSubview:self.verifiedImage];
    
    UILabel *verifiedlabel = [[UILabel alloc] initWithFrame:CGRectMake(self.verifiedImage.left-20, self.verifiedImage.bottom +5, width+40, 20)];
    verifiedlabel.font = [UIFont systemFontOfSize:14];
    verifiedlabel.adjustsFontSizeToFitWidth = YES;
    verifiedlabel.textAlignment = NSTextAlignmentCenter;
    verifiedlabel.textColor = RGB(85, 85, 85);
    verifiedlabel.text = @"实名认证";
    [footerview addSubview:verifiedlabel];
    
    self.verifiedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.verifiedBtn.frame = CGRectMake( self.verifiedImage.left, verifiedlabel.bottom+5 , self.verifiedImage.width, 25);
    self.verifiedBtn.layer.cornerRadius = 2;
    self.verifiedBtn.clipsToBounds = YES;
    [self.verifiedBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.verifiedBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.verifiedBtn addTarget:self action:@selector(verifiedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerview addSubview:self.verifiedBtn];
    
    self.avatarcerImage = [[UIImageView alloc] initWithFrame:CGRectMake(margin*2+width, certificationLabel.bottom + 15, width, width)];
    [footerview addSubview:self.avatarcerImage];
    
    UILabel *avatarcerlabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarcerImage.left-20, self.avatarcerImage.bottom +5, width+40, 20)];
    avatarcerlabel.font = [UIFont systemFontOfSize:14];
    avatarcerlabel.adjustsFontSizeToFitWidth = YES;
    avatarcerlabel.textAlignment = NSTextAlignmentCenter;
    avatarcerlabel.textColor = RGB(85, 85, 85);
    avatarcerlabel.text = @"头像认证";
    [footerview addSubview:avatarcerlabel];
    
    self.avatarcerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.avatarcerBtn.frame = CGRectMake( self.avatarcerImage.left, avatarcerlabel.bottom+5, self.avatarcerImage.width, 25);
    self.avatarcerBtn.layer.cornerRadius = 2;
    self.avatarcerBtn.clipsToBounds = YES;
    [self.avatarcerBtn setTitle:@"去认证" forState:UIControlStateNormal];
    [self.avatarcerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.avatarcerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.avatarcerBtn addTarget:self action:@selector(avatarcerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerview addSubview:self.avatarcerBtn];
    
    UILabel *promotelabel = [[UILabel alloc] initWithFrame:CGRectMake((WIDTH - 150)/2, self.avatarcerBtn.bottom + 20, 150, 21)];
    promotelabel.font = [UIFont systemFontOfSize:12];
    promotelabel.adjustsFontSizeToFitWidth = YES;
    promotelabel.textAlignment = NSTextAlignmentCenter;
    promotelabel.textColor = RGB(85, 85, 85);
    promotelabel.text = @"完成以下认证提升信用分数";
    [footerview addSubview:promotelabel];
    
    UIView *leftline = [[UIView alloc] initWithFrame:CGRectMake(margin,promotelabel.top+10, promotelabel.left - margin-5, 1)];
    leftline.backgroundColor = RGB(153, 153, 153);
    [footerview addSubview:leftline];
    
    UIView *rightline = [[UIView alloc] initWithFrame:CGRectMake(promotelabel.right+5,promotelabel.top+10, leftline.width, 1)];
    rightline.backgroundColor = RGB(153, 153, 153);
    [footerview addSubview:rightline];
    
    self.educationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.educationBtn.frame = CGRectMake( self.avatarcerImage.left, promotelabel.bottom+15, self.avatarcerImage.width, 25);
    self.educationBtn.layer.cornerRadius = 2;
    self.educationBtn.clipsToBounds = YES;
    [self.educationBtn setTitle:@"去认证" forState:UIControlStateNormal];
    [self.educationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.educationBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.educationBtn addTarget:self action:@selector(educationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerview addSubview:self.educationBtn];
    
    UILabel *educatlabel = [[UILabel alloc] initWithFrame:CGRectMake(margin, self.educationBtn.top, self.educationBtn.left-margin-5, 25)];
    educatlabel.font = [UIFont systemFontOfSize:13];
    educatlabel.adjustsFontSizeToFitWidth = YES;
    educatlabel.textColor = RGB(85, 85, 85);
    educatlabel.text = @"学历证书认证";
    [footerview addSubview:educatlabel];
    
    self.jobBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.jobBtn.frame = CGRectMake( self.avatarcerImage.left, self.educationBtn.bottom+15, self.avatarcerImage.width, 25);
    self.jobBtn.layer.cornerRadius = 2;
    self.jobBtn.clipsToBounds = YES;
    [self.jobBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.jobBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.jobBtn addTarget:self action:@selector(jobBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerview addSubview:self.jobBtn];

    UILabel *joblabel = [[UILabel alloc] initWithFrame:CGRectMake(margin, self.jobBtn.top, self.jobBtn.left-margin-5, 25)];
    joblabel.font = [UIFont systemFontOfSize:13];
    joblabel.adjustsFontSizeToFitWidth = YES;
    joblabel.textColor = RGB(85, 85, 85);
    joblabel.text = @"工作证明认证";
    [footerview addSubview:joblabel];
  
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0.001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}
- (void)requestData{
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:MyBaseInfo_Get_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDDismiss;
            weakSelf.tableview.hidden = NO;
            [DataToolsControl dataAnalysisUserBaseInfo:responseObject];
            [weakSelf showCertification:responseObject[@"data"]];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

- (void)showCertification:(NSDictionary *)data{
    self.dataDic = data;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:data[@"avatar"]]];
    self.evaluationlabel.text = [NSString stringWithFormat:@"您的职场认证 %@",data[@"credit_level_cn"]];
    self.timeLabel.text = [NSString stringWithFormat:@"评估时间：%@",data[@"credit_update"]];
    self.beatLabel.text = [NSString stringWithFormat:@"您已击败%@%%的同行",data[@"credit_surpass"]];
    
    NSMutableArray *array = [NSMutableArray array];
    //实名认证
    if ([data[@"realname_audit"] integerValue] == 0) { // 未认证
        self.verifiedImage.image = [UIImage imageNamed:@"xinyong-shiming2"];
         [self.verifiedBtn setTitle:@"去认证" forState:UIControlStateNormal];
        self.verifiedBtn.backgroundColor = [UIColor grayColor];
    } else if ([data[@"realname_audit"] integerValue] == 1) { // 已认证
        self.verifiedImage.image = [UIImage imageNamed:@"xinyong-shiming1"];
         [self.verifiedBtn setTitle:@"已认证" forState:UIControlStateNormal];
        self.verifiedBtn.backgroundColor = [UIColor orangeColor];
        [array addObject:@"1"];
    } else if ([data[@"realname_audit"] integerValue] == 2){ // 认证中
        self.verifiedImage.image = [UIImage imageNamed:@"xinyong-shiming2"];
         [self.verifiedBtn setTitle:@"认证中" forState:UIControlStateNormal];
        self.verifiedBtn.backgroundColor = [UIColor grayColor];
    } else if ([data[@"realname_audit"] integerValue] == 3) { // 认证失败
        self.verifiedImage.image = [UIImage imageNamed:@"xinyong-shiming2"];
         [self.verifiedBtn setTitle:@"认证失败" forState:UIControlStateNormal];
        self.verifiedBtn.backgroundColor = [UIColor grayColor];
    }
    
    //头像认证
    if ([data[@"avatar_audit"] integerValue] == 0) {
        //未认证
        self.avatarcerImage.image = [UIImage imageNamed:@"xinyong-touxiang2"];
        self.avatarcerBtn.backgroundColor = [UIColor grayColor];
        [self.avatarcerBtn setTitle:@"去认证" forState:UIControlStateNormal];
    } else if ([data[@"avatar_audit"] integerValue] == 1) {
        //已认证
        self.avatarcerImage.image = [UIImage imageNamed:@"xinyong-touxiang1"];
        self.avatarcerBtn.backgroundColor = [UIColor orangeColor];
        [self.avatarcerBtn setTitle:@"已认证" forState:UIControlStateNormal];
         [array addObject:@"1"];
    }
    
    //学历认证
    if ([data[@"education_audit"] integerValue] == 0) {
        //未认证
        self.educationBtn.backgroundColor = [UIColor grayColor];
        [self.educationBtn setTitle:@"去认证" forState:UIControlStateNormal];
    } else if ([data[@"education_audit"] integerValue] == 1) { // 已认证
        self.educationBtn.backgroundColor = [UIColor orangeColor];
        [self.educationBtn setTitle:@"已认证" forState:UIControlStateNormal];
         [array addObject:@"1"];
    }
    
    //工作认证
    if ([data[@"work_audit"] integerValue] == 0) { // 未认证
        self.jobBtn.backgroundColor = [UIColor grayColor];
        [self.jobBtn setTitle:@"去认证" forState:UIControlStateNormal];
    } else if ([data[@"work_audit"] integerValue] == 1) { // 已认证
        self.jobBtn.backgroundColor = [UIColor orangeColor];
        [self.jobBtn setTitle:@"已认证" forState:UIControlStateNormal];
         [array addObject:@"1"];
    }
    
    self.evaluationNumber = [data[@"credit_score"] integerValue];
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(update:) userInfo:nil repeats:YES];
}

#pragma mark -- Action
- (void)update:(NSTimer *)timer{
    if (self.circleProgessView2.value >= self.evaluationNumber){
        [timer invalidate];
    }else{
      self.circleProgessView2.value++;
    }
}

- (void)verifiedBtnClick:(UIButton *)button {
    //实名认证
    __weak typeof(self) weakSelf = self;
    RealNameAuthenticationViewController *nameVC = [[RealNameAuthenticationViewController alloc] init];
    nameVC.setok = ^(NSString *name) {
        [weakSelf requestData];
    };
    [self.navigationController pushViewController:nameVC animated:YES];
}

- (void)avatarcerBtnClick:(UIButton *)button{
    //头像认证
    LYAvatarCertificationController *avatarVC = [[LYAvatarCertificationController alloc] init];
    avatarVC.avatar = [NSString stringWithFormat:@"%@", self.dataDic[@"avatar"]];
    avatarVC.avatar_audit = [NSString stringWithFormat:@"%@", self.dataDic[@"avatar_audit"]];
    [self.navigationController pushViewController:avatarVC animated:YES];
}

- (void)educationBtnClick:(UIButton *)button{
    //教育认证
    LYAcademicCertificateListViewController *listVC = [[LYAcademicCertificateListViewController alloc] init];
    listVC.state = CertificateListStateAcademic;
    [self.navigationController pushViewController:listVC animated:YES];
}

- (void)jobBtnClick:(UIButton *)button{
    //工作认证
    LYAcademicCertificateListViewController *listVC = [[LYAcademicCertificateListViewController alloc] init];
    listVC.state = CertificateListStatePosition;
    [self.navigationController pushViewController:listVC animated:YES];
}


@end
